package com.dhwl.hsh.merchant.presenter;

import android.util.Log;
import android.widget.Toast;

import com.dhwl.hsh.lib.bean.HttpResponse;
import com.dhwl.hsh.lib.bean.resp.OrderDetailResp;
import com.dhwl.hsh.lib.core.HshException;
import com.dhwl.hsh.lib.core.mvp.BasePresenter;
import com.dhwl.hsh.lib.utils.ReservoirUtils;
import com.dhwl.hsh.lib.utils.ToastUtils;
import com.dhwl.hsh.merchant.HshApplication;
import com.dhwl.hsh.merchant.R;
import com.dhwl.hsh.merchant.model.impl.OrderModel;
import com.dhwl.hsh.merchant.presenter.iview.IOrderDetailView;
import com.dhwl.hsh.merchant.ui.activity.BaseActivity;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

/**
 * 订单详情
 *
 * @author Derek.Kidd
 * @version V1.0.0
 * @date 2016/5/16 13:44
 */
public class OrderDetailPresenter extends BasePresenter<IOrderDetailView> {
    private ReservoirUtils reservoirUtils;

    public OrderDetailPresenter() {
        this.reservoirUtils = new ReservoirUtils();
    }

    public void getOrderDetail(String orderId) {

        Subscriber<HttpResponse<OrderDetailResp>> subscriber = new Subscriber<HttpResponse<OrderDetailResp>>() {

            @Override
            public void onCompleted() {
                if (OrderDetailPresenter.this.mCompositeSubscription != null)
                    OrderDetailPresenter.this.mCompositeSubscription.remove(this);
            }

            @Override
            public void onError(Throwable e) {
                Log.e("getOrderDetail", e.getMessage());
                ((BaseActivity) OrderDetailPresenter.this.getMvpView()).hideWaitingDialog();
                ToastUtils.show(HshApplication.mContext, R.string.network_not_exist, Toast.LENGTH_SHORT);
            }

            @Override
            public void onNext(HttpResponse<OrderDetailResp> orderDetailRespHttpResponse) {
                OrderDetailResp responseDetail = orderDetailRespHttpResponse.getData();
                if (orderDetailRespHttpResponse.getStatus() != 0) {
                    HshException exception = new HshException(orderDetailRespHttpResponse.getStatus(), orderDetailRespHttpResponse.getDescribe());
                    OrderDetailPresenter.this.getMvpView().onOrderDetailFailure(exception);
                } else {
                    OrderDetailPresenter.this.getMvpView().onOrderDetailSuccess(responseDetail);
                }
            }
        };
        this.mCompositeSubscription.add(OrderModel.getInstance().getOrderDetail(orderId).subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                ((BaseActivity) OrderDetailPresenter.this.getMvpView()).showWaitingDialog();
            }
        }).doAfterTerminate(new Action0() {
            @Override
            public void call() {
                ((BaseActivity) OrderDetailPresenter.this.getMvpView()).hideWaitingDialog();
            }
        }).subscribe(subscriber));

    }

    public void cancelOrder(int type, String orderId) {
        Subscriber<HttpResponse<String>> subscriber = new Subscriber<HttpResponse<String>>() {

            @Override
            public void onCompleted() {
                if (OrderDetailPresenter.this.mCompositeSubscription != null)
                    OrderDetailPresenter.this.mCompositeSubscription.remove(this);
            }

            @Override
            public void onError(Throwable e) {
                Log.e("cancelOrder", e.getMessage());
                ((BaseActivity) OrderDetailPresenter.this.getMvpView()).hideWaitingDialog();
                ToastUtils.show(HshApplication.mContext, R.string.network_not_exist, Toast.LENGTH_SHORT);
            }

            @Override
            public void onNext(HttpResponse<String> orderDetailRespHttpResponse) {
                if (orderDetailRespHttpResponse.getStatus() != 0) {
                    HshException exception = new HshException(orderDetailRespHttpResponse.getStatus(), orderDetailRespHttpResponse.getDescribe());
                    OrderDetailPresenter.this.getMvpView().onCancelOrderFalure(exception);
                } else {
                    ToastUtils.show(HshApplication.mContext, "取消订单成功", Toast.LENGTH_SHORT);
                    OrderDetailPresenter.this.getMvpView().onCancelOrderSuc();
                }
            }
        };
        this.mCompositeSubscription.add(OrderModel.getInstance().cancelOrder(type, orderId).subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                ((BaseActivity) OrderDetailPresenter.this.getMvpView()).showWaitingDialog();
            }
        }).doAfterTerminate(new Action0() {
            @Override
            public void call() {
                ((BaseActivity) OrderDetailPresenter.this.getMvpView()).hideWaitingDialog();
            }
        }).subscribe(subscriber));
    }

}
